﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

namespace KPIBalanceScoreCard
{
    public partial class CreateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UserCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().GetData(args.Value).Rows.Count > 0)
            {
                args.IsValid = false;
                UserCustomValidator.Text = "User already registered. ";
            }
        }

        protected void UserRefValidCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (new HierarchyDatasetTableAdapters.GetEmpNameTableAdapter().GetEmployeeName(args.Value) == null)
            {
                args.IsValid = false;
            }
        }

        protected void userRefTxt_TextChanged(object sender, EventArgs e)
        {
            DataRowCollection rows = new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().GetData(userRefTxt.Text).Rows;
            if (rows != null && rows.Count > 0)
            {
                email.Text = rows[0]["email"].ToString();
                userName.Text = rows[0]["userName"].ToString();
            }
            else
            {
                email.Text = "No value found in Db";
                userName.Text = "No value found in Db";
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().UpdatePassword(Encryption.Encrypt( pwd.Text, ApplicationStrings.encryptionKey) ,userRefTxt.Text);
            Response.Redirect("LoginPage.aspx");
        }
    }
}
